/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.ui.components;

import com.bff.bjj.ui.component.MusicList;
import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import com.bff.mpd.musicjukebox.lib.MusicObjects;
import com.bff.mpd.musicjukebox.lib.actions.PlaylistInsertArtistAction;
import com.bff.mpd.utils.MPDComparator;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.bff.javampd.MPDDatabase;
import org.bff.javampd.MPDPlaylist;
import org.bff.javampd.objects.MPDArtist;

/**
 *
 * @author Bill
 */
public class ArtistList extends MusicList implements MouseListener {

    private MPDDatabase database;
    private DefaultListModel artistListModel;
    private JPopupMenu artistMenu;
    private MPDPlaylist playlist;
    private List<MPDArtist> artists;

    private static MPDComparator mpdComparator;

    public ArtistList() {
        artists = new ArrayList<MPDArtist>();
        mpdComparator = new MPDComparator();
        this.database = JukeboxProperties.getInstance().getDatabase();
        this.playlist = JukeboxProperties.getInstance().getPlaylist();

        setCellRenderer(new ArtistListCellRenderer());
        addMouseListener(this);
    }

    public ArtistList(List<MPDArtist> artists) {
        this();
        listArtists(artists);
    }

    public void setArtist(List<MPDArtist> artists) {
        listArtists(artists);
    }

    private void listArtists(List<MPDArtist> artists) {
        this.artists = new ArrayList<MPDArtist>(artists);
        Collections.sort(artists, mpdComparator);
        loadList();
    }

    public void listAll() {
        listArtists(new ArrayList<MPDArtist>(MusicObjects.getArtists()));
    }

    public void filterList(String filter) {

        if ("".equals(filter.trim())) {
            listAll();
        } else {
            try {
                artistListModel.clear();

                for (MPDArtist artist : artists) {
                    if (artist.getName().toLowerCase().startsWith(filter.toLowerCase())) {
                        artistListModel.addElement(artist);
                    }
                }

                setSelectedIndex(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void clearFilter() {
        loadList();
    }

    public MPDArtist getSelectedArtist() {
        if (getSelectedValue() == null) {
            return null;
        }

        return (MPDArtist) getSelectedValue();
    }

    public MPDDatabase getDatabase() {
        return database;
    }

    public void setDatabase(MPDDatabase database) {
        this.database = database;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            //MPDArtist artist = getSelectedArtist();
            //new ArtistDialog(Utils.getParentComponent(this), artist);
        } else {
            createArtistPlayPopUp(e);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        createArtistPlayPopUp(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        createArtistPlayPopUp(e);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    private void createArtistPlayPopUp(MouseEvent e) {
        if (e.isPopupTrigger()) {
            setSelectedIndex(locationToIndex(e.getPoint()));
            MPDArtist artist = getSelectedArtist();
            artistMenu = new JPopupMenu();
            artistMenu.add(new JMenuItem(new PlaylistInsertArtistAction(getPlaylist(), artist)));
            //artistMenu.add(new JMenuItem(new ViewArtistAction(Utils.getParentComponent(this), artist)));
            artistMenu.setVisible(true);
            artistMenu.show(e.getComponent(), e.getX(), e.getY());
        }
    }

    public MPDPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(MPDPlaylist playlist) {
        this.playlist = playlist;
    }

    private void loadList() {
        artistListModel = new DefaultListModel();
        try {
            for (MPDArtist artist : artists) {
                artistListModel.addElement(artist);
            }
            setModel(artistListModel);
            setSelectedIndex(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
